﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDFHelper
{
    internal class PDFHelper
    {
        /// <summary>
        /// 创建PDF
        /// </summary>
        public static void CreatPDF<T>(string title, List<T> dataLists)
        {
            string PDFSavePath = "";
            Document document = new Document(PageSize.A4);//创建一个Document实例
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFSavePath, FileMode.Create));//创建Writer实例

            document.Open();

            #region 写入一些数据，包括：作者、标题、摘要、关键词、创建者、生产者、创建时间、头信息

            //document.AddAuthor("lhg");
            //document.AddCreationDate();
            //document.AddCreator("addcare");
            //document.AddHeader("QQ", "123456");
            //document.AddHeader("Email", "123456");
            //document.AddKeywords("物联网");
            //document.AddProducer();
            //document.AddSubject("VIP观众入场凭券!");
            //document.AddTitle("2010 年深圳物联网技术和应用展览会VIP观众信息");

            #endregion

            BaseFont baseFont = CreateChineseFont();
            //字体设置
            iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 22, Font.BOLD);
            iTextSharp.text.Font fontUnderLine = new iTextSharp.text.Font(baseFont, 12, Font.UNDERLINE);
            iTextSharp.text.Font normalFont = new Font(baseFont, 12);
            iTextSharp.text.Font normalRedFont = new Font(baseFont, 12, Font.NORMAL | Font.BOLD, BaseColor.RED);

            float titleLineHeight = 45f, normalLineHeight = 25f;
            Paragraph pBlank = new Paragraph(" ", normalFont);
            pBlank.Leading = normalLineHeight;

            //图片
            Image jpeg = Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "Data\\logo.bmp");
            jpeg.Alignment = Element.ALIGN_CENTER;
            document.Add(jpeg);

            //标题
            Paragraph titleP = new Paragraph(title, titleFont);
            titleP.Leading = titleLineHeight; titleP.Alignment = Element.ALIGN_CENTER;
            document.Add(titleP);

            Chunk chunk1 = new Chunk("尊敬的", normalFont), chunk2 = new Chunk("  xxx  ", fontUnderLine)
                , chunk3 = new Chunk("：", normalFont);
            document.Add(GetPTxt(new Chunk[] { chunk1, chunk2, chunk3 }, normalLineHeight));

            Chunk chnk4 = new Chunk("        感谢您登记成为 2010 年深圳国际物联网技术与应用展览会的 VIP 观众，以下您的注册信息及 VIP 编号，", normalFont);
            Chunk chnk5 = new Chunk("请将此表打印出来，带至展会现场，作为入场凭证：", normalRedFont);
            document.Add(GetPTxt(new Chunk[] { chnk4, chnk5 }, normalLineHeight));
            document.Add(pBlank);

            //表格生成
            document.Add(CreateTable(dataLists));

            document.Add(pBlank);//空行
            document.Add(GetPTxt("其实生成PDF就是这么简单！！", normalFont, normalLineHeight));

            document.Close();
        }

        /// <summary>
        /// 创建中文字体(实现中文)
        /// </summary>
        /// <returns></returns>
        public static BaseFont CreateChineseFont()
        {
            //使用.dll文件字体
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H", 
            BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

            //使用系统自带字体
            //BaseFont baseFT= BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            return baseFT;
        }

        #region 文档元素生成区域

        /// <summary>
        /// 生成段落文本
        /// </summary>
        /// <param name="txt">文本</param>
        /// <param name="txtFont">字体</param>
        /// <param name="lineHeight">行高</param>
        /// <returns>段落文本</returns>
        static Paragraph GetPTxt(string txt, Font txtFont, float lineHeight)
        {
            Paragraph p = new Paragraph(lineHeight, txt, txtFont);
            return p;
        }

        /// <summary>
        /// 生成段落文本
        /// </summary>
        /// <param name="txt">文本</param>
        /// <param name="txtFont">字体</param>
        /// <param name="lineHeight">行高</param>
        /// <param name="elementAlign">对齐方式</param>
        /// <returns>段落文本</returns>
        static Paragraph GetPTxt(string txt, Font txtFont, float lineHeight, int elementAlign)
        {
            Paragraph p = new Paragraph(lineHeight, txt, txtFont);
            p.Alignment = elementAlign;
            return p;
        }

        /// <summary>
        /// 生成段落文本
        /// </summary>
        /// <param name="chnkArr">Chunk数组</param>
        /// <param name="lineHeight">行高</param>
        /// <returns>段落文本</returns>
        static Paragraph GetPTxt(Chunk[] chnkArr, float lineHeight)
        {
            if (chnkArr == null || chnkArr.Length == 0) { return new Paragraph(""); }
            Paragraph p = new Paragraph();
            foreach (Chunk chnkTxt in chnkArr) { p.Add(chnkTxt); }
            p.Leading = lineHeight;
            return p;
        }

        /// <summary>
        /// 创建Table行
        /// </summary>
        /// <param name="txt">文本</param>
        /// <param name="txtFont">字体</param>
        /// <param name="align">对齐方式</param>
        /// <param name="colSpan">跨行数</param>
        /// <param name="padTop">顶部padding</param>
        /// <param name="padBottom">底部padding</param>
        /// <returns>Table行</returns>
        static PdfPCell CreateCell(string txt, Font txtFont, int align, int colSpan, float padTop, float padBottom)
        {
            PdfPCell cell = new PdfPCell(new Phrase(txt, txtFont));
            if (padTop > 0) { cell.PaddingTop = padTop; }
            if (padBottom > 0) { cell.PaddingBottom = padBottom; }
            if (colSpan > 0) { cell.Colspan = colSpan; }
            cell.HorizontalAlignment = align;

            return cell;
        }

        /// <summary>
        /// 创建Table行(RowSpan)
        /// </summary>
        /// <param name="txt">文本</param>
        /// <param name="txtFont">字体</param>
        /// <returns>Table行(RowSpan)</returns>
        static PdfPCell CreateRowSpanCell(string txt, Font txtFont)
        {
            PdfPCell cell = new PdfPCell(new Phrase(60f, txt, txtFont));
            cell.Rowspan = 6;
            cell.PaddingTop = 6f;
            cell.PaddingBottom = 6f;
            cell.PaddingLeft = 10f;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;

            return cell;
        }

        /// <summary>
        /// 创建Table行
        /// </summary>
        /// <param name="txt">文本</param>
        /// <param name="txtFont">字体</param>
        /// <param name="colSpan">跨行数</param>
        /// <param name="padTop">顶部padding</param>
        /// <param name="padBottom">底部padding</param>
        /// <param name="bgColor">背景色</param>
        /// <returns>Table行</returns>
        static PdfPCell CreateCellHeader(string txt, Font txtFont, int colSpan, float padTop, float padBottom, BaseColor bgColor)
        {
            PdfPCell cell = new PdfPCell(new Phrase(txt, txtFont));
            if (padTop > 0) { cell.PaddingTop = padTop; }
            if (padBottom > 0) { cell.PaddingBottom = padBottom; }
            if (colSpan > 0) { cell.Colspan = colSpan; }
            cell.HorizontalAlignment = Element.ALIGN_CENTER; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.BackgroundColor = bgColor;

            return cell;
        }

        /// <summary>
        /// 创建表格(PdfPTable)
        /// </summary>
        /// <returns></returns>
        static PdfPTable CreateTable<T>(List<T> dataLists)
        {
            //表格设置
            PdfPTable table = new PdfPTable(6);//表格列数
            table.TotalWidth = 470f;//表格总宽度
            table.LockedWidth = true;
            table.HorizontalAlignment = 1;

            float[] widths = new float[] { 95f, 95f, 55f, 40f, 95f, 95f };//表格每列宽度设置
            table.SetWidths(widths);

            //
            BaseFont baseFont = CreateChineseFont();
            iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 12, Font.BOLD);
            iTextSharp.text.Font normalFont = new Font(baseFont, 10), normalBoldFont = new Font(baseFont, 10, Font.BOLD);
            iTextSharp.text.Font normalRedFont = new Font(baseFont, 10, Font.NORMAL | Font.BOLD, BaseColor.RED);
            float padding = 6f; BaseColor bgColor = new BaseColor(153, 204, 255);
            //表格数据填充
            //foreach(T dataList in dataLists)
            //{

            //}
            for (int i = 0; i < dataLists.Count; i++)
            {
                table.AddCell(CreateCell("  ", normalFont, 0, 0, padding, padding));
                table.AddCell(CreateCell("  ", normalFont, 0, 2, padding, padding));
                table.AddCell(CreateCell("  ", normalFont, 0, 0, padding, padding));
                table.AddCell(CreateCell("  ", normalFont, 0, 0, padding, padding));
            }

            return table;
        }

        #endregion
    }
}
